package com.fh.shop.api.recipient.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConstant;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.recipient.biz.IRecipientService;
import com.fh.shop.api.recipient.po.Recipient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/recipients")
@Api(tags = "收件人接口类")
public class RecipientController {

    @Resource(name = "recipientService")
    private IRecipientService recipientService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping
    @NeedLogin
    @ApiOperation("添加收件人")
    @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    public ServerResponse addRecipient(@RequestBody Recipient recipient) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long id = memberVo.getId();
        recipient.setMemberId(id);
        return recipientService.addRecipient(recipient);
    }

    @GetMapping
    @NeedLogin
    @ApiOperation("查询收件人列表")
    @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    public ServerResponse findList() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long id = memberVo.getId();
        return recipientService.findList(id);
    }

    @DeleteMapping("/{id}")
    @NeedLogin
    @ApiOperation("删除收件人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "id", value = "收件人id", example = "0")
    })
    public ServerResponse deleteRecipient(@PathVariable Long id) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return recipientService.deleteRecipient(id, memberId);
    }

    @GetMapping("/{id}")
    @NeedLogin
    @ApiOperation("查询收件人根据id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "id", value = "收件人id", example = "0")
    })
    public ServerResponse findRecipient(@PathVariable Long id) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return recipientService.findRecipient(id, memberId);
    }

    @PutMapping
    @NeedLogin
    @ApiOperation("更新收件人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    })
    public ServerResponse updateRecipient(@RequestBody Recipient recipient) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        recipient.setMemberId(memberId);
        return recipientService.updateRecipient(recipient);
    }

    @PostMapping("/{id}")
    @NeedLogin
    @ApiOperation("更新收件人状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "id", value = "收件人id", example = "0")
    })
    public ServerResponse updateStatus(@PathVariable Long id) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return recipientService.updateStatus(memberId, id);
    }
 }
